import { _decorator, Component, Node, Label, director } from 'cc';
const { ccclass, property } = _decorator;

import SSS_GameEventManager, { SS_GameEventData } from "../../../../Script/Manager/SSS_GameEventManager";
import { DragTetris_GameData } from "../Data/DragTetris_GameData";
import { DragTetris_GameStorage } from "../Data/DragTetris_GameStorage";
import DragTetris_UIManager from "../Manager/DragTetris_UIManager";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

@ccclass('DragTetrisGamePanel')
export default class DragTetris_GamePanel extends Component {
    static Instance: DragTetris_GamePanel;
    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_refresh: Node | null = null;
    @property(Label)
    Label_score: Label | null = null;
    @property(Label)
    Label_highScore: Label | null = null;
    GeneratePoint_blook_list = [];
    GeneratePoint_blook_list_temp = [];
    onLoad() {
        DragTetris_GamePanel.Instance = this;

        this.GeneratePoint_blook_list = DragTetris_UIManager.Instance.GeneratePoint_blook.children;

        this.GeneratePoint_blook_list_temp = DragTetris_UIManager.Instance.GeneratePoint_blook_temp.children;
    }
    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    Refresh() {
        this.Label_score.string = `${DragTetris_GameData.GetScore()}`;
        this.Label_highScore.string = `${DragTetris_GameStorage.data.high_score}`;
    }
    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }
    RemoveListener() {
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }
    On_Button_back() {
        director.loadScene("MainScene");

        SSS_AudioManager.PlayButtonAudio();
    }
    On_Button_refresh() {
        // let currentSceneName = director.getScene().name;
        // director.loadScene(currentSceneName);

        // DragTetris_GameManager.Clear();

        // DragTetris_GameData.SaveGridData();

        DragTetris_GameStorage.data.map.isEnd = true;
        DragTetris_GameStorage.saveData();

        SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Replay);

        SSS_AudioManager.PlayButtonAudio();
    }
}

// /** 调试 */
window["DragTetris_GamePanel"] = DragTetris_GamePanel;